package com.chc.exer;

/**
 * @Author: chc
 * @Date: 2024/11/10/16:48
 * @Description: 售票问题
 */
public class TicketRunnable {


    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread t1 = new Thread(ticket);
        t1.setName("窗口1");
        Thread t2 = new Thread(ticket);
        t2.setName("窗口2");
        Thread t3 = new Thread(ticket);
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }



    static class Ticket implements Runnable{
        private int ticket = 100;
        @Override
        public void run() {
            while (true){
                if (ticket-- < 0) {
                    System.out.println(Thread.currentThread().getName() + "售票买完了");
                    break;
                }
                show();
            }
        }

        private synchronized void show() {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票");
                ticket--;
            }
        }

    }
}
